package com.amkai.mobile.client.options;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.amkai.mobile.client.AeMobile;
import com.amkai.mobile.client.R;
import com.amkai.mobile.client.common.AEMobileConstants;
import com.amkai.mobile.client.common.SharedDataProvider;
import com.amkai.mobile.utils.data.Constants;
import com.amkai.mobile.utils.data.MobileDb;

public class ConnectionSetup extends Activity {
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.connection_setup);
		setTitle(AEMobileConstants.APP_TITLE  +" - " + "Connection Setup");
		Button btn_save = (Button) findViewById(R.id.btnsave);
		

		loadData();
		final EditText server = (EditText) findViewById(R.id.server);

		final EditText username = (EditText) findViewById(R.id.username);
		final EditText password = (EditText) findViewById(R.id.password);
	
		btn_save.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				SharedDataProvider.reset();
				MobileDb
						.insertConnection(ConnectionSetup.this, username.getText().toString(), password.getText().toString(), server.getText().toString());
				AeMobile.startMain(ConnectionSetup.this);

			}
		});

		
	}

	private void loadData() {
		String serverTxt = MobileDb.getLoginParameter(this, Constants.SERVER);
		String usernameTxt = MobileDb.getLoginParameter(this, Constants.USERNAME);
		String passwordTxt = MobileDb.getLoginParameter(this, Constants.PASSWORD);
		EditText server = (EditText) findViewById(R.id.server);
		server.setText(serverTxt);
		EditText username = (EditText) findViewById(R.id.username);
		username.setText(usernameTxt);
		EditText password = (EditText) findViewById(R.id.password);
		password.setText(passwordTxt);
	}

}
